package bsh.utils.reflection.transform;

import org.objectweb.asm.MethodVisitor;

import bsh.CompilationState;
import bsh.exception.CompilationFailedException;
import bsh.meta.TypeTransformation;
import bsh.meta.TypeWrapper;

/**
 * @author takeshi
 */
public class NullTypeTransformationFactory implements TypeTransformationFactory {

    /**
     * {@inheritDoc}
     */
    public TypeTransformation getTypeTransformation(TypeWrapper fromType, final TypeWrapper toType) {
        return new TypeTransformation() {

            public TypeWrapper transformStackTop(MethodVisitor visitor, TypeWrapper fromType, CompilationState state)
                    throws CompilationFailedException {
                state.assertStackTopType(fromType);
                state.popStack();
                state.pushStack(toType);
                return toType;
            }

            public TypeWrapper toType(TypeWrapper fromType) {
                return toType;
            }

            public boolean acceptsType(TypeWrapper type) {
                return type == TypeWrapper.NULL_TYPE_WRAPPER;
            }
        };
    }

    /**
     * {@inheritDoc}
     */
    public boolean isApplicable(TypeWrapper fromType, TypeWrapper toType) {
        return fromType == TypeWrapper.NULL_TYPE_WRAPPER;
    }

}
